package com.distribute.framework.model;

import com.distribute.framework.core.web.helper.Utils;
import java.lang.reflect.Field;
import java.util.Date; 
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import javax.annotation.Resource;

import com.distribute.framework.core.web.util.Page;
import com.distribute.framework.core.lang.*; 
import com.distribute.framework.core.web.helper.WebHelper.LzMap;
import com.distribute.framework.core.util.Function;
import com.distribute.framework.core.util.IbatisMap;
import com.distribute.framework.config.*;
import com.distribute.framework.plugin.*;

@namespace("permissions/group")
public @caption("组织部门") @database(DatabaseNames.distribute_sys) @source("distribute_sys") @table("srv_group") class SrvGroup implements java.io.Serializable
{
	private static final long serialVersionUID = 1l;
	private @caption("编号") @column("sg_id")    @primary  @notnull  @note("  ") Long sgId;// 主键 非空     
	private @caption("组名称") @column("sg_name")     @notnull  @note("  ") String sgName;// 非空     
	private @caption("上级组") @column("sg_parent")     @notnull  @relate("$sgParent") @RelateClass(SrvGroup.class)  @note("  ") Long sgParent;// 非空     
 	private transient SrvGroup $sgParent;//关联对象[组织部门]
	private @caption("族序列") @column("sg_family") @like     @note("  ") String sgFamily;//     
	private @caption("负责人") @column("sg_user")      @relate("$sgUser") @RelateClass(SrvUser.class)  @note("  ") Long sgUser;//    /admin/permissions/user.query.do 
 	private transient SrvUser $sgUser;//关联对象[人员信息]
	private @caption("组标识") @column("sg_flag")      @note("  ") String sgFlag;//     
	private @caption("状态") @column("sg_status")     @notnull  @note(" 1:启用 0:禁用  ") Short sgStatus;// 非空 1:启用 0:禁用     
	 
	//默认构造函数
	public SrvGroup(){ 
	
	}
	
	//主键构造函数
	public SrvGroup(Long id){
		this.sgId = id;
	}
	
	/**所有字段构造函数 
	 SrvGroup(
	 	Utils.getString("sgName")//组名称 [非空]
	 	,Utils.getLong("sgParent")//上级组 [非空]
	 	,Utils.getString("sgFamily")//族序列
	 	,Utils.getLong("sgUser")//负责人
	 	,Utils.getString("sgFlag")//组标识
	 	,Utils.getShort("sgStatus")//状态 [非空]
	 )
	**/
	public SrvGroup(String sgName,Long sgParent,String sgFamily,Long sgUser,String sgFlag,Short sgStatus){
		this.sgName=sgName;
		this.sgParent=sgParent;
		this.sgFamily=sgFamily;
		this.sgUser=sgUser;
		this.sgFlag=sgFlag;
		this.sgStatus=sgStatus;
	}
	
	//设置非空字段
	public SrvGroup setNotNull(String sgName,Long sgParent,Short sgStatus){
		this.sgName=sgName;
		this.sgParent=sgParent;
		this.sgStatus=sgStatus;
		return this;
	}
	/** 编号 [非空]       **/
	public SrvGroup sgId(Long sgId){
		this.sgId = sgId;
		this.setSeted(F.sgId);
		return this;
	}
	/** 组名称 [非空]       **/
	public SrvGroup sgName(String sgName){
		this.sgName = sgName;
		this.setSeted(F.sgName);
		return this;
	}
	/** 上级组 [非空] [SrvGroup]      **/
	public SrvGroup sgParent(Long sgParent){
		this.sgParent = sgParent;
		this.setSeted(F.sgParent);
		return this;
	}
	/** 族序列        **/
	public SrvGroup sgFamily(String sgFamily){
		this.sgFamily = sgFamily;
		this.setSeted(F.sgFamily);
		return this;
	}
	/** 负责人  [SrvUser]     /admin/permissions/user.query.do **/
	public SrvGroup sgUser(Long sgUser){
		this.sgUser = sgUser;
		this.setSeted(F.sgUser);
		return this;
	}
	/** 组标识        **/
	public SrvGroup sgFlag(String sgFlag){
		this.sgFlag = sgFlag;
		this.setSeted(F.sgFlag);
		return this;
	}
	/** 状态 [非空]   1:启用 0:禁用     **/
	public SrvGroup sgStatus(Short sgStatus){
		this.sgStatus = sgStatus;
		this.setSeted(F.sgStatus);
		return this;
	}
	
	
	/**
	* 克隆对象
	**/
	public SrvGroup clone(){
		SrvGroup clone = new SrvGroup();
		clone.sgName=this.sgName;
		clone.sgParent=this.sgParent;
		clone.sgFamily=this.sgFamily;
		clone.sgUser=this.sgUser;
		clone.sgFlag=this.sgFlag;
		clone.sgStatus=this.sgStatus;
		return clone;
	}
	/**
	* 清除缓存
	**/
	public void clear(){
		Cache.clearObject(SrvGroup.M.class);
		Cache.clearValue(SrvGroup.M.class);
		Cache.deleteObject(SrvGroup.class, this.getId());
	}
	
	/**
	* 清除缓存
	**/
	public static void clear(Long id){
		Cache.clearObject(SrvGroup.M.class);
		Cache.clearValue(SrvGroup.M.class);
		Cache.deleteObject(SrvGroup.class, id);
	}
	
	
	/**
	 * 根据ID取组织部门
	 * @param id
	 * @return
	 */
	public static @api SrvGroup get(Long id){		
		return getSrvGroupById(id);
	}
	/**
	 * 设置ID
	 * @param id
	 * @return
	 */
	public void setId(Long id){		
		this.sgId = id;
	}
	
	/**
	 * 获取所有组织部门
	 * @return
	 */
	public static @api List<SrvGroup> list(Map params,Integer size){
		return getSrvGroupList(params,size);
	}
	/**
	 * 获取组织部门分页
	 * @return
	 */
	public static @api Page<SrvGroup> page(Integer page,Integer size,Map params){
		return getSrvGroupPage(page, size , params);
	}
	
	
	/**
	 * 根据查询条件取组织部门
	 * @param params
	 * @return
	 */
	public static @api SrvGroup Get(Map params){
		return getSrvGroup(params);
	}
	/**
	 * 获取组织部门数
	 * @return
	 */
	public static @api Long count(Map params){
		return getSrvGroupCount(params);
	}
	/**
	 * 获取组织部门数
	 * @return
	 */
	public static @api <T> T eval(String eval,Map params){
		return getSrvGroupEval(eval,params);
	}
	/**
	 * 获取组织部门图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> stats(Map params,C sumKey,C groupKey){
		return getSrvGroupStats(params,sumKey,groupKey,-1);
	}
	/**
	 * 获取组织部门图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 分组的字段
	 * @param dateType 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> stats(Map params,C sumKey,C groupKey,int dateType){
		return getSrvGroupStats(params,sumKey,groupKey,dateType);
	}
	
	/**
	 * 根据ID取组织部门
	 * @param id
	 * @return
	 */
	public static @api SrvGroup getSrvGroupById(Long id){		
		return getSrvGroupDao().getSrvGroupById(id);
	}
	/**
	 * 获取id
	 * @return
	 */
	public Long getId(){
		return this.sgId;
	}
	
	
	/**
	 * 根据ID取组织部门的标识键值
	 * @param id
	 * @return
	 */
	public static @api String getKeyValue(Object key){
		Long id = Utils.parseLong(key);	
		if(id==null || id.longValue()==0 ){
			return "[无]";
		}
		String value = null;
		value = Cache.getValue(SrvGroup.class, id);
		if(value==null){
			SrvGroup srvGroup = get(id);
			if(srvGroup!=null){
				String strValue = srvGroup.getSgName$();
				if(!"SgName".equals("SgName"))
					strValue+="("+srvGroup.getSgName$()+")";
				Cache.setValue(SrvGroup.class, id ,strValue);
				value = strValue;
			}else{
				Cache.setValue(SrvGroup.class, id ,"{"+id+"}",30);
				value = "{"+id+"}";
			}
		}
		/************LAZY3Q_ID_KEYVALUE************/
		/************LAZY3Q_ID_KEYVALUE************/
		return value;
	}	
	
	
	/**
	 * 获取当前对象的键值
	 * @return
	 */
	public String getKeyValue(){
		/************LAZY3Q_CODE_KEYVALUE************/
		/************LAZY3Q_CODE_KEYVALUE************/
		String keyValue = this.getSgName$();
		if(!"SgName".equals("SgName"))
			keyValue+="("+this.getSgName$()+")";
		return keyValue;
	}
	
	
	/**
	 * 根据关键字精确查询组织部门对象
	 * @return
	 */
	public static @api SrvGroup find(String key){
		if(Utils.empty(key))return null;
		Map params = new HashMap();
		if(key.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
			params.put("sgId", key.toLowerCase().replaceFirst("id:", ""));					
		else{//按标识查询，精确查询
			String strDefinex = "";
			strDefinex+=" or sg_name = '"+key.replaceAll("'", "''")+"'";
			strDefinex="(2=1 "+strDefinex+")";
			params.put("definex",strDefinex);
		}
		/************LAZY3Q_CODE_FIND************/
		/************LAZY3Q_CODE_FIND************/
		return SrvGroup.getSrvGroup(params);
	}
	
	
	/**
	 * 根据关键字查询组织部门
	 * @return
	 */
	public static @api List<Map> query(String key,int size,Map params,Boolean bObject){
		if(Utils.empty((String)params.get("asc")) && Utils.empty((String)params.get("desc")))//如果未传入排序字段
			params.put("desc","sg_id");//那么，默认以主键降序，传给Dao层
		String strTerm=key;		
		if(!Utils.empty(strTerm)){
			if(strTerm.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
				params.put("sgId", strTerm.toLowerCase().replaceFirst("id:", ""));					
			else//按标识查询，模糊查询请带%
			{
				String strDefinex = "";
				strDefinex+=" or sg_name like '%"+strTerm.replaceAll("'", "''")+"%'";
				strDefinex="(2=1 "+strDefinex+")";
				params.put("definex",strDefinex);
			}
		}
		/************LAZY3Q_CODE_QUERY************/
		/************LAZY3Q_CODE_QUERY************/
			
		List<SrvGroup> list = page(0,size,params).getResult();
		
		/**
		* OK!取到数据拼成放入Map中，[{value,text,object:{...}},...]
		* value:数据ID，text:数据标识,object,整个对象
		**/
		List<Map> result = new java.util.ArrayList();
		for(SrvGroup srvGroup:list){
			Map map = new HashMap();
			map.put("key",srvGroup.getSgId().toString());
			map.put("value", Utils.js(srvGroup.getKeyValue()));
			if(bObject==true)
				map.put("object",srvGroup);
			result.add(map);
		}
		/************LAZY3Q_AFTER_QUERY************/
		/************LAZY3Q_AFTER_QUERY************/
		
		return result;
	}
	
	
	
	/**
	 * 获取所有组织部门
	 * @return
	 */
	public static @api List<SrvGroup> getSrvGroupList(Map params,Integer size){
		return getSrvGroupDao().getSrvGroupList(params,0,size);
	}
	
	/**
	 * 获取组织部门分页
	 * @return
	 */
	public static @api Page<SrvGroup> getSrvGroupPage(Integer page,Integer size,Map params){
		if(page==null)page=0;
		if(size==null)size=20;
		return getSrvGroupDao().getSrvGroupPage(page, size , params);
	}
	
	/**
	 * 根据查询条件取组织部门
	 * @param params
	 * @return
	 */
	public static @api SrvGroup getSrvGroup(Map params){
		return getSrvGroupDao().getSrvGroup(params);
	}
	
	/**
	 * 保存一个组织部门
	 * @param srvGroup
	 * @return
	 */
	public static @api SrvGroup saveSrvGroup(SrvGroup srvGroup){
		return getSrvGroupDao().saveSrvGroup(srvGroup);
	}
	
	/**
	 * 根据条件更新组织部门
	 * @param values
	 * @param params
	 */
	public static @api int updateSrvGroupByConfirm(Map values, Map params){
		return getSrvGroupDao().updateSrvGroupByConfirm(values,params);
	}
	
	/**
	 * 修改一个组织部门
	 * @param srvGroup
	 * @return
	 */
	public static @api int updateSrvGroup(SrvGroup srvGroup){
		return getSrvGroupDao().updateSrvGroup(srvGroup);
	}
	
	/**
	 * 更新组织部门非空字段
	 * @param srvGroup
	 */
	public static @api int updateSrvGroup$NotNull(SrvGroup srvGroup){
		return getSrvGroupDao().updateSrvGroup$NotNull(srvGroup);
	}
	
	/**
	 * 获取组织部门数
	 * @return
	 */
	public static @api Long getSrvGroupCount(Map params){
		return getSrvGroupDao().getSrvGroupCount(params);
	}
		
		
	/**
	 * 获取组织部门自定义求和表达式,比如求和:eval="sum(id)"
	 * @return
	 */	
	public static @api <T> T getSrvGroupEval(String eval,Map params){
		return getSrvGroupDao().getSrvGroupEval(eval,params);
	}
	/**
	 * 获取组织部门图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 分组的字段
	 * @param dateType 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> getSrvGroupStats(Map params,C sumKey,C groupKey,int dateType){
		//TODO:图表统计功能未实现
		
		Map<String,Object> groups=new HashMap();//要分组的字段
		Map<String,Object> sums=new HashMap();//要统计的字段
		
		String[][] dateFormats=new String[][]{
				null,
				new String[]{"%Y","yyyy年","Y"},
				new String[]{"%Y-%m","yyyy年MM月","M"},
				new String[]{"%Y%U","yyyy年第ww周","W"},
				new String[]{"%Y-%m-%d","yyyy年MM月dd日","D"},
				new String[]{"%Y-%m-%d %H","yyyy年MM月dd日 HH点","H"},
				new String[]{"%m","MM月"},
				new String[]{"%d","dd号"},
				new String[]{"%H","HH点"} 
		};
		
		//获取要分组的字段，放入Map中
		if(dateType>0){
			groups.put(groupKey.toString(),"DATE_FORMAT({key},'"+dateFormats[dateType][0]+"')");
		}else{
			groups.put(groupKey.toString(),"{key}");
		}
		if(dateType<6 && dateType>0){ 
			params.put("asc", groupKey.toString()); 
			if(params.get(groupKey.name()+"Start")==null){
				params.put(groupKey.name()+"Start", Utils.date(new Date(), -24, dateFormats[dateType][2]));
			}
		}
		
		
		//获取要统计的字段，放入Map中
		if(sumKey.toString().equals("count")){
			sums.put(sumKey.toString(),"count(*)");
		}else{
			sums.put(sumKey.toString(),"sum({key})");
		}
		
		List<Map> list = getSrvGroupDao().getSrvGroupStats(params,groups,sums);
		
		List<Map> result = new ArrayList();
		for(Map m:list){
			Map map = new HashMap();
			Object value = m.get(sumKey.toString());
			Object name = null;
			Object groupValue = m.get(groupKey.toString());
			if(dateType>0){
				name = Utils.date(Utils.parseDate(groupValue), dateFormats[dateType][1]);
			}else{
				try{
					java.lang.reflect.Method method = SrvGroup.class.getMethod("get"+Utils.ForMat(groupKey.toString())+"$");
					method.setAccessible(true);
					name = method.invoke(SrvGroup.fromMap(m));
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
			map.put("name", name);
			map.put("value", value);
			result.add(map);
		}
		return result;
	}
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static int updateSrvGroup(Map set,Map where){
		if(set.isEmpty())throw new RuntimeException("set为空");
		if(where.isEmpty())throw new RuntimeException("where为空");
		where.put("confirm", "1");
		return getSrvGroupDao().updateSrvGroupByConfirm(set, where);
	}
	
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static void deleteSrvGroup(Map where){
		if(where.isEmpty())throw new RuntimeException("where为空");
		where.put("confirm", "1");
		getSrvGroupDao().deleteSrvGroupByConfirm(where);
	}
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static void delete(Long id){
		if(id==null)throw new RuntimeException("id为空");
		getSrvGroupDao().removeSrvGroupById(id);
	}
	
	
	/**
	 * 保存组织部门对象
	 * @param params
	 * @return
	 */
	public SrvGroup save(){
		if(this.getSgId()!=null)
			getSrvGroupDao().updateSrvGroup(this);
		else
			return getSrvGroupDao().saveSrvGroup(this);
		return this;
	}
	
	
	/**
	 * 更新组织部门对象的非空字段,注意跟Service里面的update不一样哦
	 * @param params
	 * @return
	 */
	public int update(){
		return getSrvGroupDao().updateSrvGroup$NotNull(this);
	}
	
	
	/**
	 * 删除
	 * @param params
	 * @return
	 */
	public void delete(){
		if(Utils.equals(Utils.config("logic_delete"),"true"))
			getSrvGroupDao().removeSrvGroupById(this.getSgId());
		else
			getSrvGroupDao().deleteSrvGroupById(this.getSgId());
	}
	
	/**
	 * 执行事务
	 * @param function
	 */
	public static <T> T execute(Function function){
		return getSrvGroupDao().executeTransaction(function);
	}
	/*******************************编号**********************************/	
	/**
	* 编号 [非空]      
	**/
	public Long getSgId(){
		if(this.sgId!=null){
			return this.sgId;
		}
		return this.sgId;
	}
	/**
	* 获取编号格式化(toString)
	**/
	public String getSgId$(){
		String strValue="";
		 strValue=Utils.string(this.getSgId());
	 	 return strValue;
	}
	/**
	* 根据编号反格式化
	**/
	public void setSgId$(String strValue){
		if(Utils.empty(strValue)){
			this.sgId=null;
			return;
		}
	 	this.sgId=Utils.parseLong(strValue);
	}
	
	/**
	* 编号 [非空]      
	**/
	public void setSgId(Long sgId){
		this.sgId = sgId;
		this.setSeted(F.sgId);
	}
	
	
	
	
	/*******************************组名称**********************************/	
	/**
	* 组名称 [非空]      
	**/
	public String getSgName(){
		if(this.sgName!=null){
			return this.sgName;
		}
		return this.sgName;
	}
	/**
	* 获取组名称格式化(toString)
	**/
	public String getSgName$(){
		String strValue="";
		 strValue=Utils.string(this.getSgName());
	 	 return strValue;
	}
	/**
	* 根据组名称反格式化
	**/
	public void setSgName$(String strValue){
		if(Utils.empty(strValue)){
			this.sgName=null;
			return;
		}
	 	this.sgName=strValue;
	}
	
	/**
	* 组名称 [非空]      
	**/
	public void setSgName(String sgName){
		this.sgName = sgName;
		this.setSeted(F.sgName);
	}
	
	
	
	
	/*******************************上级组**********************************/	
	/**
	* 上级组 [非空] [SrvGroup]     
	**/
	public Long getSgParent(){
		if(this.sgParent!=null){
			return this.sgParent;
		}
		return this.sgParent;
	}
	/**
	* 获取上级组格式化(toString)
	**/
	public String getSgParent$(){
		String strValue="";
		if(this.getSgParent()!=null){
				strValue+=Utils.string(SrvGroup.getKeyValue(this.getSgParent()));
		}			
	 	 return strValue;
	}
	/**
	* 根据上级组反格式化
	**/
	public void setSgParent$(String strValue){
		if(Utils.empty(strValue)){
			this.sgParent=null;
			return;
		}
		SrvGroup item = SrvGroup.find(strValue);
		if(item!=null){
			this.sgParent = Utils.parseLong(item.getSgId());
		}
	}
	
	/**
	* 上级组 [非空] [SrvGroup]     
	**/
	public void setSgParent(Long sgParent){
		this.sgParent = sgParent;
		this.setSeted(F.sgParent);
	}
	
	
	
	
	/**
	* 获取关联对象[组织部门]
	**/	 			
 	public SrvGroup get$sgParent(){
 		if(this.$sgParent!=null)
 			return this.$sgParent;
 		com.distribute.framework.model.SrvGroup srvGroup = null;
		if(this.getSgParent()!=null){
 			srvGroup = SrvGroup.get(this.getSgParent());
 		}
 		return srvGroup;
	}
	/*******************************族序列**********************************/	
	/**
	* 族序列       
	**/
	public String getSgFamily(){
		if(this.sgFamily!=null){
			return this.sgFamily;
		}
		return this.sgFamily;
	}
	/**
	* 获取族序列格式化(toString)
	**/
	public String getSgFamily$(){
		String strValue="";
		 strValue=Utils.string(this.getSgFamily());	
	 	 return strValue;
	}
	/**
	* 根据族序列反格式化
	**/
	public void setSgFamily$(String strValue){
		if(Utils.empty(strValue)){
			this.sgFamily=null;
			return;
		}
		 //此处未实现
		 this.sgFamily=Utils.parseString(strValue);
	}
	
	/**
	* 族序列       
	**/
	public void setSgFamily(String sgFamily){
		this.sgFamily = sgFamily;
		this.setSeted(F.sgFamily);
	}
	
	
	
	
	/*******************************负责人**********************************/	
	/**
	* 负责人  [SrvUser]     /admin/permissions/user.query.do
	**/
	public Long getSgUser(){
		if(this.sgUser!=null){
			return this.sgUser;
		}
		return this.sgUser;
	}
	/**
	* 获取负责人格式化(toString)
	**/
	public String getSgUser$(){
		String strValue="";
		if(this.getSgUser()!=null){
				strValue+=Utils.string(SrvUser.getKeyValue(this.getSgUser()));
		}			
	 	 return strValue;
	}
	/**
	* 根据负责人反格式化
	**/
	public void setSgUser$(String strValue){
		if(Utils.empty(strValue)){
			this.sgUser=null;
			return;
		}
		SrvUser item = SrvUser.find(strValue);
		if(item!=null){
			this.sgUser = Utils.parseLong(item.getSuId());
		}
	}
	
	/**
	* 负责人  [SrvUser]     /admin/permissions/user.query.do
	**/
	public void setSgUser(Long sgUser){
		this.sgUser = sgUser;
		this.setSeted(F.sgUser);
	}
	
	
	
	
	/**
	* 获取关联对象[人员信息]
	**/	 			
 	public SrvUser get$sgUser(){
 		if(this.$sgUser!=null)
 			return this.$sgUser;
 		com.distribute.framework.model.SrvUser srvUser = null;
		if(this.getSgUser()!=null){
 			srvUser = SrvUser.get(this.getSgUser());
 		}
 		return srvUser;
	}
	/*******************************组标识**********************************/	
	/**
	* 组标识       
	**/
	public String getSgFlag(){
		if(this.sgFlag!=null){
			return this.sgFlag;
		}
		return this.sgFlag;
	}
	/**
	* 获取组标识格式化(toString)
	**/
	public String getSgFlag$(){
		String strValue="";
		 strValue=Utils.string(this.getSgFlag());
	 	 return strValue;
	}
	/**
	* 根据组标识反格式化
	**/
	public void setSgFlag$(String strValue){
		if(Utils.empty(strValue)){
			this.sgFlag=null;
			return;
		}
	 	this.sgFlag=strValue;
	}
	
	/**
	* 组标识       
	**/
	public void setSgFlag(String sgFlag){
		this.sgFlag = sgFlag;
		this.setSeted(F.sgFlag);
	}
	
	
	
	
	/*******************************状态**********************************/	
	/**
	* 状态 [非空]   1:启用 0:禁用    
	**/
	public Short getSgStatus(){
		if(this.sgStatus!=null){
			return this.sgStatus;
		}
		return this.sgStatus;
	}
	/**
	* 获取状态格式化(toString)
	**/
	public String getSgStatus$(){
		String strValue="";
		 if(Utils.equals(Utils.string(this.getSgStatus()),"1"))
			strValue=Utils.string("启用");		 
		 if(Utils.equals(Utils.string(this.getSgStatus()),"0"))
			strValue=Utils.string("禁用");		 
	 	 return strValue;
	}
	/**
	* 根据状态反格式化
	**/
	public void setSgStatus$(String strValue){
		if(Utils.empty(strValue)){
			this.sgStatus=null;
			return;
		}
		 if(Utils.equals(strValue,"启用"))
		 	this.sgStatus=Utils.parseShort("1");
		 if(Utils.equals(strValue,"禁用"))
		 	this.sgStatus=Utils.parseShort("0");
	}
	
	/**
	* 状态 [非空]   1:启用 0:禁用    
	**/
	public void setSgStatus(Short sgStatus){
		this.sgStatus = sgStatus;
		this.setSeted(F.sgStatus);
	}
	
	
	
	
	/************LAZY3Q_DEFINE_CODE************/
	/************LAZY3Q_DEFINE_CODE************/
	
	
	/**
	 * 触发器
	 */
	public static void on(Trigger.Index index,final Notify.Listener listener,Trigger.Type... types){
		Notify.on(SrvGroup.class, index, listener, types);
	}
	public static void on(final Notify.Listener listener,String type){
		Notify.on(SrvGroup.class, listener, type);
	}
	/**
	 * 当修改字段时
	 * @param filedName
	 * @param listener
	 */
	public static void on(String filedName,final Notify.Listener listener){
		Notify.on(SrvGroup.class, listener, filedName);
	}
	/**
	 * 当修改字段为某值时
	 * @param filedName
	 * @param value
	 * @param listener
	 */
	public static void on(String filedName,Object value,final Notify.Listener listener){
		if(value==null)return;
		Notify.on(SrvGroup.class, listener, filedName+"."+Utils.md5(""+value));
	}
	public void notify(String type){
		Trigger.on(SrvGroup.class,type, this);
	}
	
	
	/**
	 * 用来获取当有字段关联对象时的获取方式,调用如${SrvGroup.obj.filedName}
	 * 因为有些变态的模板语言不能识别$，所以不能以${SrvGroup.$filedName}获取关联对象
	 */
	public Map getObj(){
		return new HashMap(){
			@Override
			public Object get(Object key) {
				try {
					return SrvGroup.class.getMethod("get$"+Utils.forMat(key.toString())).invoke(SrvGroup.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	/**
	 * 用来获取某字段格式化的获取方式,调用如${SrvGroup.fmt.filedName}
	 * 因为有些变态的模板语言不能识别$，所以不能以${SrvGroup.filedName$}获取某字段格式化
	 */
	public Map getFmt(){
		return new HashMap(){
			@Override
			public Object get(Object key) {
				try {
					return SrvGroup.class.getMethod("get"+Utils.ForMat(key.toString()+"$")).invoke(SrvGroup.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	/**
	 * 获取查询参数链式串
	 * @return
	 */
	public static @Deprecated M where(){ return new M(0); }
	public static @Deprecated M m(){ return new M(0); }
	public static M where = new M(0,true);
	public static @database(DatabaseNames.distribute_sys) class M extends IbatisMap{
		int type = 0; 
		M linked = null;
		Boolean proxy = false;
		public M set = null;
		public M(int type){
			this.type = type;
			if(this.type==0){
				set = new M(1,true);
				set.linked = this;
			}
		};
		public M(int type,Boolean proxy){
			this.type = type;
			this.proxy=proxy;
		};
		public M proxy(){
			M m = new M(this.type);//当前的对像
			if(this.type==0){
				m.set = new M(1,true);//set对象
				m.set.linked = m;
			}else{
				m.linked=this.linked;
				this.linked.linked=m;
			}
			return m;
		}
		public @Deprecated M set(){			
			M m = this;
			m.linked=new M(1);
			m.linked.linked=m;
			return m.linked;
		};
		public M on(IbatisMap value,C column,String joinColumn){if(proxy)return proxy().on(value,column,joinColumn);this.put(value.getClass().getDeclaringClass().getSimpleName(), value);value.put("on", column.toString());value.put("to", joinColumn);return this;};
		/** 编号 [非空]       **/
		public M sgId(Object sgId){if(proxy)return proxy().sgId(sgId);  this.put("sgId", sgId);return this;};
	 	/** and sg_id is null */
 		public M sgIdNull(){if(proxy)return proxy().sgIdNull();if(this.get("sgIdNot")==null)this.put("sgIdNot", "");this.put("sgId", null);return this;};
 		/** not .... */
 		public M sgIdNot(){if(proxy)return proxy().sgIdNot();this.put("sgIdNot", "not");return this;};
 		public M sgIdNotNull(){if(proxy)return proxy().sgIdNotNull();this.put("sgIdNot", "not");this.put("sgId", null);return this;};
 		public M sgIdNot(Object sgId){if(proxy)return proxy().sgIdNot(sgId);this.put("sgIdNot", "not");this.put("sgId", sgId);return this;};
		/** and sg_id <= ? */
		public M sgIdMin(Object min){if(proxy)return proxy().sgIdMin(min);this.put("sgIdMin", min);return this;};
		/** and sg_id >= ? */
		public M sgIdMax(Object max){if(proxy)return proxy().sgIdMax(max);this.put("sgIdMax", max);return this;};
		public M sgIdIn(String ids){
			if(proxy)return proxy().sgIdIn(ids);  this.put("sgIdIn", ids);return this;
		};	
		/** 组名称 [非空]       **/
		public M sgName(Object sgName){if(proxy)return proxy().sgName(sgName);  this.put("sgName", sgName);return this;};
	 	/** and sg_name is null */
 		public M sgNameNull(){if(proxy)return proxy().sgNameNull();if(this.get("sgNameNot")==null)this.put("sgNameNot", "");this.put("sgName", null);return this;};
 		/** not .... */
 		public M sgNameNot(){if(proxy)return proxy().sgNameNot();this.put("sgNameNot", "not");return this;};
 		public M sgNameNotNull(){if(proxy)return proxy().sgNameNotNull();this.put("sgNameNot", "not");this.put("sgName", null);return this;};
 		public M sgNameNot(Object sgName){if(proxy)return proxy().sgNameNot(sgName);this.put("sgNameNot", "not");this.put("sgName", sgName);return this;};
		public M sgParent$on(SrvGroup.M value){if(proxy)return proxy().sgParent$on(value);this.put("SgParent", value);value.put("on", "sg_parent");return this;};
		/** 上级组 [非空] [SrvGroup]      **/
		public M sgParent(Object sgParent){if(proxy)return proxy().sgParent(sgParent);  this.put("sgParent", sgParent);return this;};
	 	/** and sg_parent is null */
 		public M sgParentNull(){if(proxy)return proxy().sgParentNull();if(this.get("sgParentNot")==null)this.put("sgParentNot", "");this.put("sgParent", null);return this;};
 		/** not .... */
 		public M sgParentNot(){if(proxy)return proxy().sgParentNot();this.put("sgParentNot", "not");return this;};
 		public M sgParentNotNull(){if(proxy)return proxy().sgParentNotNull();this.put("sgParentNot", "not");this.put("sgParent", null);return this;};
 		public M sgParentNot(Object sgParent){if(proxy)return proxy().sgParentNot(sgParent);this.put("sgParentNot", "not");this.put("sgParent", sgParent);return this;};
		public M sgParentIn(String ids){
			if(proxy)return proxy().sgParentIn(ids);  this.put("sgParentIn", ids);return this;
		};	
		/** 族序列        **/
		public M sgFamily(Object sgFamily){if(proxy)return proxy().sgFamily(sgFamily);  this.put("sgFamily", sgFamily);return this;};
	 	/** and sg_family is null */
 		public M sgFamilyNull(){if(proxy)return proxy().sgFamilyNull();if(this.get("sgFamilyNot")==null)this.put("sgFamilyNot", "");this.put("sgFamily", null);return this;};
 		/** not .... */
 		public M sgFamilyNot(){if(proxy)return proxy().sgFamilyNot();this.put("sgFamilyNot", "not");return this;};
 		public M sgFamilyNotNull(){if(proxy)return proxy().sgFamilyNotNull();this.put("sgFamilyNot", "not");this.put("sgFamily", null);return this;};
 		public M sgFamilyNot(Object sgFamily){if(proxy)return proxy().sgFamilyNot(sgFamily);this.put("sgFamilyNot", "not");this.put("sgFamily", sgFamily);return this;};
		public M sgUser$on(SrvUser.M value){if(proxy)return proxy().sgUser$on(value);this.put("SgUser", value);value.put("on", "sg_user");return this;};
		/** 负责人  [SrvUser]     /admin/permissions/user.query.do **/
		public M sgUser(Object sgUser){if(proxy)return proxy().sgUser(sgUser);  this.put("sgUser", sgUser);return this;};
	 	/** and sg_user is null */
 		public M sgUserNull(){if(proxy)return proxy().sgUserNull();if(this.get("sgUserNot")==null)this.put("sgUserNot", "");this.put("sgUser", null);return this;};
 		/** not .... */
 		public M sgUserNot(){if(proxy)return proxy().sgUserNot();this.put("sgUserNot", "not");return this;};
 		public M sgUserNotNull(){if(proxy)return proxy().sgUserNotNull();this.put("sgUserNot", "not");this.put("sgUser", null);return this;};
 		public M sgUserNot(Object sgUser){if(proxy)return proxy().sgUserNot(sgUser);this.put("sgUserNot", "not");this.put("sgUser", sgUser);return this;};
		public M sgUserIn(String ids){
			if(proxy)return proxy().sgUserIn(ids);  this.put("sgUserIn", ids);return this;
		};	
		/** 组标识        **/
		public M sgFlag(Object sgFlag){if(proxy)return proxy().sgFlag(sgFlag);  this.put("sgFlag", sgFlag);return this;};
	 	/** and sg_flag is null */
 		public M sgFlagNull(){if(proxy)return proxy().sgFlagNull();if(this.get("sgFlagNot")==null)this.put("sgFlagNot", "");this.put("sgFlag", null);return this;};
 		/** not .... */
 		public M sgFlagNot(){if(proxy)return proxy().sgFlagNot();this.put("sgFlagNot", "not");return this;};
 		public M sgFlagNotNull(){if(proxy)return proxy().sgFlagNotNull();this.put("sgFlagNot", "not");this.put("sgFlag", null);return this;};
 		public M sgFlagNot(Object sgFlag){if(proxy)return proxy().sgFlagNot(sgFlag);this.put("sgFlagNot", "not");this.put("sgFlag", sgFlag);return this;};
		/** 状态 [非空]   1:启用 0:禁用     **/
		public M sgStatus(Object sgStatus){if(proxy)return proxy().sgStatus(sgStatus);  this.put("sgStatus", sgStatus);return this;};
	 	/** and sg_status is null */
 		public M sgStatusNull(){if(proxy)return proxy().sgStatusNull();if(this.get("sgStatusNot")==null)this.put("sgStatusNot", "");this.put("sgStatus", null);return this;};
 		/** not .... */
 		public M sgStatusNot(){if(proxy)return proxy().sgStatusNot();this.put("sgStatusNot", "not");return this;};
 		public M sgStatusNotNull(){if(proxy)return proxy().sgStatusNotNull();this.put("sgStatusNot", "not");this.put("sgStatus", null);return this;};
 		public M sgStatusNot(Object sgStatus){if(proxy)return proxy().sgStatusNot(sgStatus);this.put("sgStatusNot", "not");this.put("sgStatus", sgStatus);return this;};
		public M sgStatusIn(String ids){
			if(proxy)return proxy().sgStatusIn(ids);  this.put("sgStatusIn", ids);return this;
		};	
	 	public M add(String key, Object value) {if(proxy)return proxy().add(key,value);this.put(key, value);return this;}
	 	public M add(F key, Object value) {if(proxy)return proxy().add(key,value);this.put(key.toString(), value);return this;}
	 	public M desc(C key) {if(proxy)return proxy().desc(key);this.put("desc", key.column);return this;}
	 	public M asc(C key) {if(proxy)return proxy().asc(key);this.put("asc", key.column);return this;}
	 	public M desc(String sql) {if(proxy)return proxy().desc(sql);this.put("desc", sql);return this;}
	 	public M asc(String sql) {if(proxy)return proxy().asc(sql);this.put("asc", sql);return this;}
	 	public M definex(String sql) {if(proxy)return proxy().definex(sql);this.put("definex", sql);return this;}
	 	/** 获取所有组织部门 **/
		public @api List<SrvGroup> list(Integer size){			
			return getSrvGroupList(this,size);
		}
		/** 获取组织部门分页 **/
		public @api Page<SrvGroup> page(Integer page,Integer size){			
			return getSrvGroupPage(page, size , this);
		}
		/** 根据查询条件取组织部门 **/
		public @api SrvGroup get(){			
			return getSrvGroup(this);
		}
		/** 获取组织部门数 **/
		public @api Long count(){			
			return getSrvGroupCount(this);
		}
		/** 获取组织部门表达式 **/
		public @api <T> T eval(String strEval){			
			return getSrvGroupEval(strEval,this);
		}
		/** 获取组织部门统计 **/
		public @api List<Map> stats(C sumKey,C groupKey,int dateType){			
			return getSrvGroupStats(this,sumKey,groupKey,dateType);
		}
		/** 获取组织部门统计 **/
		public @api List<Map> stats(C sumKey,C groupKey){			
			return getSrvGroupStats(this,sumKey,groupKey,-1);
		}
		/** 根据条件更新 **/
		public @api int update(){			
			M where = null;
			M set = null;
			if(this.type==0){
				if(this.linked==null)throw new RuntimeException("未发set");
				where = this;
				set = this.linked;
			}else{
				if(this.linked==null)throw new RuntimeException("未发where条件");
				set = this;
				where = this.linked;
			}
			return updateSrvGroup(set,where);
		}
		/** 根据条件删除 **/
		public @api void delete(){			
			deleteSrvGroup(this);
		}
	}
	
	public static class Param extends M{
		public Param(int type) {
			super(type);
		}
		public Param(int type, Boolean proxy) {
			super(type, proxy);
		}
	}
	
	/** 对象的字段描述 **/
	public static class F{
		/** 编号 [非空]       **/
		public final static @type(Long.class)  String sgId="sgId";
		/** 组名称 [非空]       **/
		public final static @type(String.class)  String sgName="sgName";
		/** 上级组 [非空] [SrvGroup]      **/
		public final static @type(Long.class)  String sgParent="sgParent";
		/** 族序列        **/
		public final static @type(String.class) @like String sgFamily="sgFamily";
		/** 负责人  [SrvUser]     /admin/permissions/user.query.do **/
		public final static @type(Long.class)  String sgUser="sgUser";
		/** 组标识        **/
		public final static @type(String.class)  String sgFlag="sgFlag";
		/** 状态 [非空]   1:启用 0:禁用     **/
		public final static @type(Short.class)  String sgStatus="sgStatus";
	 	public static String get(String name){
			try {
				Field field = F.class.getField(name);
				if(field!=null)
					return field.get(null).toString();
			} catch (Exception e) {
				//e.printStackTrace();
			}
			return null;
		}
	}
	/** 对象的数据库字段描述 **/
	public static enum C{
		sgId("sg_id"),
		sgName("sg_name"),
		sgParent("sg_parent"),
		sgFamily("sg_family"),
		sgUser("sg_user"),
		sgFlag("sg_flag"),
		sgStatus("sg_status"),
		count("count");
		String column;
		C(String column){
			this.column = column;
		}
		public String toString(){
			return this.column;
		}
		public static String get(String name){
			try {
				Field field = C.class.getField(name);
				if(field!=null)
					return field.get(null).toString();
			} catch (Exception e) {
				//e.printStackTrace();
			}
			return null;
		}
	}
	
	
	
	/**
	* 获取组织部门数据
	**/
	public static Object getData(Object param){
		if(param==null)
			return null;
		Object value = null;
		if(param.getClass()==Long.class)
			value = SrvGroup.get((Long)param);
		else if(param instanceof java.util.Map){
			java.util.Map params = (Map) param;
			if(params.get("size")==null)
				value = SrvGroup.getSrvGroup(params);
			else
				value = SrvGroup.getSrvGroupList(params, (Integer) params.get("size"));
		}else if(param.getClass()==Long.class )
			value =  SrvGroup.where().sgId(param).get();
		else if(!Utils.empty(param.toString()))
			value = SrvGroup.get(Long.valueOf(param.toString()));
		return value;
	}
	
	/**对象转json**/
	public Map toJSON(){
		return toJSON(false);
	}
	public Map toJSON(boolean simple){
		Map map = new HashMap();
		map.put("id",this.getSgId());
		Utils.fms(map,simple?"id":"sgId",this.getSgId(),this.getSgId$());
		Utils.fms(map,simple?"name":"sgName",this.getSgName(),this.getSgName$());
		Utils.fms(map,simple?"parent":"sgParent",this.getSgParent(),this.getSgParent$());
		Utils.fms(map,simple?"family":"sgFamily",this.getSgFamily(),this.getSgFamily$());
		Utils.fms(map,simple?"user":"sgUser",this.getSgUser(),this.getSgUser$());
		Utils.fms(map,simple?"flag":"sgFlag",this.getSgFlag(),this.getSgFlag$());
		Utils.fms(map,simple?"status":"sgStatus",this.getSgStatus(),this.getSgStatus$());
		if(this.suffix!=null)map.put("suffix", this.suffix);
		/************LAZY3Q_DEFINE_TOJSON************/
		/************LAZY3Q_DEFINE_TOJSON************/
		return map;
	}
	/**对象转map**/
	public Map toMap(){
		Map map = new HashMap();
		map.put("sgId",this.getSgId());
		map.put("sgName",this.getSgName());
		map.put("sgParent",this.getSgParent());
		map.put("sgFamily",this.getSgFamily());
		map.put("sgUser",this.getSgUser());
		map.put("sgFlag",this.getSgFlag());
		map.put("sgStatus",this.getSgStatus());
		map.put("suffix", this.suffix);
		/************LAZY3Q_DEFINE_TOMAP************/
		/************LAZY3Q_DEFINE_TOMAP************/
		return map;
	}
	/**map转对象**/
	public static SrvGroup fromMap(Map map){
		SrvGroup srvGroup = new SrvGroup();
		srvGroup.sgId = Utils.parseLong(map.get("sgId"));
		srvGroup.sgName = Utils.parseString(map.get("sgName"));
		srvGroup.sgParent = Utils.parseLong(map.get("sgParent"));
		srvGroup.sgFamily = Utils.parseString(map.get("sgFamily"));
		srvGroup.sgUser = Utils.parseLong(map.get("sgUser"));
		srvGroup.sgFlag = Utils.parseString(map.get("sgFlag"));
		srvGroup.sgStatus = Utils.parseShort(map.get("sgStatus"));
		return srvGroup;
	}
	
	
	/**字段设置标记队列**/
	private Map<String,Boolean> seted=new HashMap();
	public Boolean getSeted(String key){
		return seted.get(key);
	}
	public void setSeted(String key){
		seted.put(key,true);
	}
	/**同步未set的字段**/
	public void mergeSet(SrvGroup old){
		Field[] fields = this.getClass().getDeclaredFields();
		try {
			for(Field field:fields){
				String key = field.getName();
				if(seted.get(key)==null && field.getAnnotation(column.class)!=null){
					field.setAccessible(true);
					field.set(this, field.get(old));					
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
		
	/**自定义变量存储**/
	private Map values=new HashMap();
	public Map getValues(){
		return values;
	}
	public void setValues(String name,Object value){
		values.put(name,value);
	}
	
	/**分表时的表名后缀**/
	private String suffix;
	public String getSuffix() {
		return suffix;
	}
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}
	/**获取数据库名称**/
	public static String getDataBaseName(){
		return Utils.config("spring.datasource.distribute_sys.name");
	}
	
	public static String getIcon(){
		return "/images/icons/2008112220121595877809.png";
	}
	public static String getIconfont(){
		return "icon-xietong2";
	}
	public static enum ACT{
		列表,添加,修改,详情
	};
	public static String getUrl(ACT act,SrvGroup.M params){
		if(params==null)params=new SrvGroup.M(0);
		params.remove("joins");
		params.remove("definexs");
		params.remove("distinct");
		String strUrl = "/admin/permissions/group.edit.html?";
		if(params.get("sgId")!=null){
			strUrl+="&id="+Utils.encode(""+params.get("sgId"),Utils.UTF8);
		}
		if(act==ACT.列表){
			strUrl = "/admin/permissions/group.html?1=1";
		}else if(act==ACT.添加){
			strUrl+="&edittype=insert";
		}else if(act==ACT.修改){
			strUrl+="&edittype=update";
		}else if(act==ACT.详情){
			strUrl+="&edittype=detail";
		}		
		if(params!=null){
			for(Object key:params.keySet()){
				strUrl+="&"+key+"="+Utils.encode(""+params.get(key),Utils.UTF8)+"";
			}
		}
		return strUrl;
	}
	public String getDetailUrl(){
		return this.getUrl(ACT.详情, where.sgId(this.getId()));
	}
	
	/**
	* 转跳到当前地址
	**/
	public static Protocol redirect(SrvGroup.ACT act,SrvGroup.M params){
		Protocol protocol = Protocol.create(true,null,"ACT_REDIRECT");
		protocol.set("url",SrvGroup.getUrl(act, params));
		return protocol;
	}
	/**
	* 打开一个新的tab页面
	**/
	public static Protocol href(SrvGroup.ACT act,SrvGroup.M params){
		Protocol protocol = Protocol.create(true,null,"ACT_HREF");
		protocol.set("url",SrvGroup.getUrl(act, params));
		protocol.set("title",act.name());
		return protocol;
	}
	/**
	* 显示一个消息框，并且关闭当前的对话框
	**/
	public static Protocol message(String message){
		Protocol protocol = Protocol.create(true,null,"ACT_MESSAGE");
		protocol.set("message",message);
		return protocol;
	}
	
	
	static com.distribute.framework.dao.ISrvGroupDao getSrvGroupDao(){
		return Utils.getBean("srvGroupDao");
	};
	
}